package com.my.fp.list;

import com.my.bean.User;
import com.my.utils.Utils;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author: marshal
 * @date: 2021-09-29 2:12 PM
 */
public class ListMapTest {
    public static void main(String[] args) {
        var list = List.of(1, 2, 3, 4, 5);
        Optional<Integer> max = list.stream().max(Integer::compareTo);
        System.out.println(max.get());
//        List<Integer> newList = list.stream().map(i -> i * i).collect(Collectors.toList());
//        System.out.println(newList);
        var userList = Utils.getUsers();
        // 求年龄和
//        int sum = userList.stream().mapToInt(User::getAge).sum();
        // 求用户年龄List
//        List<Integer> ageList = userList.stream().map(User::getAge).collect(Collectors.toList());

//        testGroupby(userList);


    }

    private static void testGroupby(List<User> userList) {
        // 分组
        Map<Integer, List<User>> userMapByAge = userList.stream().collect(Collectors.groupingBy(User::getAge));
        userMapByAge.keySet().forEach(e -> {
            System.out.println(userMapByAge.get(e).stream().collect(Collectors.toMap(User::getName, User::getAge)));
        });
    }

}
